package cn.udrm.water.base.v1.administration.restful;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.udrm.water.base.v1.administration.dto.AdministrationUnitDto;
import cn.udrm.water.base.v1.administration.service.AdministrationUnitService;
import cn.udrm.water.base.v1.commons.models.PageModel;
import cn.udrm.water.base.v1.commons.models.RequestListParam;
import cn.udrm.water.base.v1.commons.models.ResponseParam;
import cn.udrm.water.base.v1.commons.models.ZTreeNodeVo;

@Api(tags="行政区划实体API", description="行政区划CRUD操作")
@RestController
@RequestMapping(value="v1/restful/administrationUnit")
public class AdministrationUnitRestful {
	@Autowired
	private AdministrationUnitService administrationUnitService;
	
	@ApiOperation("获取行政区划树结构（一次性加载）")
	@GetMapping(value="getAdministrationUnitZtree")
	public ResponseParam<List<ZTreeNodeVo>> getAdministrationUnitZtree() {
		List<ZTreeNodeVo> list = administrationUnitService.getAdministrationUnitZtree();
		return new ResponseParam<List<ZTreeNodeVo>>("数据获取成功", 200, list);
	}

	@ApiOperation("列表数据获取")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="body", name="requestListParam", required=false, value="列表查询条件", dataType="ResponseParam«AdministrationUnitDto»")
	})
	@RequestMapping(value="list", method={RequestMethod.GET, RequestMethod.POST})
	public ResponseParam<PageModel<AdministrationUnitDto>> list(@RequestBody(required=false) RequestListParam<AdministrationUnitDto> requestListParam) {
		if (requestListParam == null) {
			requestListParam = new RequestListParam<>();
		}
		PageModel<AdministrationUnitDto> pageByConditions = administrationUnitService.getPageByConditions(requestListParam.getNumber()
													,requestListParam.getSize()
													,requestListParam.getEntity()
													,requestListParam.getSortField()
													,requestListParam.getSortType());
		return new ResponseParam<PageModel<AdministrationUnitDto>>("数据获取成功", 200, pageByConditions);
	}
	
	@ApiOperation("单条数据获取")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="行政区划GUID")
	@GetMapping(value="get/{id}")
	public ResponseParam<AdministrationUnitDto> get(@PathVariable(value="id") String id) {
		AdministrationUnitDto administrationDto = administrationUnitService.getAllById(id);
		return new ResponseParam<AdministrationUnitDto>("数据获取成功", 200, administrationDto);
	}
	
	@ApiOperation("行政区划数据保存")
	@ApiImplicitParam(name="administrationDto", value="需要添加的行政区划", required=true, dataType="AdministrationUnitDto", dataTypeClass=AdministrationUnitDto.class)
	@PostMapping(value="save")
	public ResponseParam<AdministrationUnitDto> save(@RequestBody AdministrationUnitDto administrationDto) {
		administrationDto = administrationUnitService.save(administrationDto);
		
		return new ResponseParam<AdministrationUnitDto>("数据保存成功", 200, administrationDto);
	}
	
	@ApiOperation("行政区划数据更新")
	@ApiImplicitParam(name="administrationDto", value="需要更新的行政区划", required=true, dataType="AdministrationUnitDto", dataTypeClass=AdministrationUnitDto.class)
	@PostMapping(value="update")
	public ResponseParam<AdministrationUnitDto> update(@RequestBody AdministrationUnitDto administrationDto) {
		administrationDto = administrationUnitService.update(administrationDto);
		
		return new ResponseParam<AdministrationUnitDto>("数据更新成功", 200, administrationDto);
	}
	
	@ApiOperation("行政区划数据删除（仅删除自身，不能删除标识有实体间关系）")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="行政区划GUID")
	@GetMapping(value="deleteForce/{id}")
	public ResponseParam<String> deleteForce(@PathVariable(value="id") String id) {
		try {
			administrationUnitService.deleteAllSelf(new String[]{id});
		} catch (Exception e) {
			return new ResponseParam<String>("删除失败", 500);
		}
		return new ResponseParam<String>("删除成功", 200);
	}
}
